//
//  HomePageViewController.m
//  07-HomeWork
//
//  Created by dllo on 16/7/22.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "HomePageViewController.h"
#define kScreenBounds [UIScreen mainScreen].bounds
#define kScreenWidth  kScreenBounds.size.width
#define kScreenHeight kScreenBounds.size.height

@interface HomePageViewController () <UIScrollViewDelegate,UITextFieldDelegate>
@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic ,strong)UIPageControl *pageControll;
@property (nonatomic, strong)UITextField *searchTextField;
@end

@implementation HomePageViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self createScrollView];
    [self createTextField];
}

- (void)createTextField {
    self.searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 20, kScreenWidth - 20, 50)];
    self.searchTextField.background = [UIImage imageNamed:@"textFieldBackGroundImage"];
    self.searchTextField.layer.cornerRadius = 25;
    self.searchTextField.clipsToBounds = YES;
    self.searchTextField.delegate = self;
    [self.view addSubview:self.searchTextField];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    textField.background = nil;
    self.scrollView.alpha = 0;
    self.pageControll.alpha = 0;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField endEditing:YES];
    self.scrollView.alpha = 1;
    self.pageControll.alpha = 1;
    self.searchTextField.background = [UIImage imageNamed:@"textFieldBackGroundImage"];
    return YES;
}
// 点击空白处收起键盘
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (![self.searchTextField isExclusiveTouch]) {
        [self.searchTextField resignFirstResponder];
        self.scrollView.alpha = 1;
        self.pageControll.alpha = 1;
        self.searchTextField.background = [UIImage imageNamed:@"textFieldBackGroundImage"];
    }
}

- (void)createScrollView {
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 600)];
    self.scrollView.backgroundColor = [UIColor redColor];
    self.scrollView.delegate = self;
    self.scrollView.contentOffset = CGPointMake(1 * kScreenWidth, 0);
    [self.view addSubview:self.scrollView];
    self.pageControll = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    self.pageControll.center = CGPointMake(kScreenWidth / 2, 600 - 10);
    self.pageControll.numberOfPages = 2;
//    pageControll.backgroundColor = [UIColor redColor];
    self.pageControll.pageIndicatorTintColor = [UIColor colorWithRed:1.000 green:0.935 blue:0.685 alpha:1.000];
    self.pageControll.currentPageIndicatorTintColor = [UIColor colorWithRed:0.00 green:0.71 blue:0.55 alpha:1.00];
    [self.view addSubview:self.pageControll];
    [self.pageControll addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];
    self.imageArray = @[@"welcome_4_1", @"welcome_4_0", @"welcome_4_1", @"welcome_4_0"];
    
        for (int i = 0; i < self.imageArray.count; i++) {
    if ([self.imageArray.firstObject isKindOfClass:[NSString class]] ) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * kScreenWidth, 0, kScreenWidth, 600)];
        imageView.image = [UIImage imageNamed:self.imageArray[i]];
        [self.scrollView addSubview:imageView];
    } else if ([self.imageArray.firstObject isKindOfClass:[UIImage class]]) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * kScreenWidth, 0, kScreenWidth, 600)];
        imageView.image = self.imageArray[i];
        [self.scrollView addSubview:imageView];
    }
    }     
    self.scrollView.contentSize = CGSizeMake(self.imageArray.count * kScreenWidth, 600);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
}
- (void)pageChange:(UIPageControl *)page {
    self.scrollView.contentOffset = CGPointMake(self.pageControll.currentPage * kScreenWidth, 0);
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    NSInteger i = scrollView.contentOffset.x / kScreenWidth;
    if (i == 0) {
        self.scrollView.contentOffset = CGPointMake(self.imageArray.count - 1 - 2, 0);
        self.pageControll.currentPage = self.imageArray.count - 3;
    } else if (i == (self.imageArray.count - 1)) {
        self.scrollView.contentOffset = CGPointMake(1 * kScreenWidth, 0);
        self.pageControll.currentPage = 0;
    } else {
    self.pageControll.currentPage = i - 1;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
